package com.sita.bike.task;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

import com.sita.bike.event.ImageLoadEvent;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by manhongyun on 15/11/12.
 */
public class ResourePicturesLoadTask extends AsyncTask<Void, Void, Void> {
    //    private ImageAdapter adapter;
    private List<String> mUrls;

    public ResourePicturesLoadTask(List<String> urls) {
        this.mUrls = urls;
    }

    //    public ImageLoadTask(ImageAdapter adapter) {
//        this.adapter = adapter;
//    }
//
//    @Override
    protected Void doInBackground(Void... params) {
        String url;
        for (int i = 0; i < mUrls.size(); i++) {
            url = mUrls.get(i);
//            ImageBean bean = (ImageBean) adapter.getItem(i);
            Bitmap bitmap = BitmapFactory
                    .decodeStream(getStream(url));

            EventBus.getDefault().post(new ImageLoadEvent(bitmap));
            //  publishProgress(); // 通知去更新UI
        }
        return null;
    }

    //
    public void onProgressUpdate(Void... voids) {

    }

    public InputStream getStream(String urlStr) {
        InputStream inStream = null;
        try {
            URL url = new URL(urlStr);
            URLConnection conn = url.openConnection();
            conn.setConnectTimeout(10 * 1000);
            inStream = conn.getInputStream();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return inStream;
    }
}
